{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating new acquisitions for GPyOpt\n",
    "\n",
    "### Written by Javier Gonzalez, University of Sheffield.\n",
    "\n",
    "## Reference Manual index\n",
    "\n",
    "*Last updated Friday, 11 Jun 2016.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can create and use your own aquisition functions in GPyOpt. To do it just complete the following template."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from GPyOpt.acquisitions.base import AcquisitionBase\n",
    "from GPyOpt.core.task.cost import constant_cost_withGradients\n",
    "    \n",
    "class AcquisitionNew(AcquisitionBase):\n",
    "    \n",
    "    \"\"\"\n",
    "    General template to create a new GPyOPt acquisition function\n",
    "\n",
    "    :param model: GPyOpt class of model\n",
    "    :param space: GPyOpt class of domain\n",
    "    :param optimizer: optimizer of the acquisition. Should be a GPyOpt optimizer\n",
    "    :param cost_withGradients: function that provides the evaluation cost and its gradients\n",
    "\n",
    "    \"\"\"\n",
    "\n",
    "    # --- Set this line to true if analytical gradients are available\n",
    "    analytical_gradient_prediction = False\n",
    "\n",
    "    \n",
    "    def __init__(self, model, space, optimizer, cost_withGradients=None, **kwargs):\n",
    "        self.optimizer = optimizer\n",
    "        super(AcquisitionNew, self).__init__(model, space, optimizer)\n",
    "        \n",
    "        # --- UNCOMMENT ONE OF THE TWO NEXT BITS\n",
    "             \n",
    "        # 1) THIS ONE IF THE EVALUATION COSTS MAKES SENSE\n",
    "        #\n",
    "        # if cost_withGradients == None:\n",
    "        #     self.cost_withGradients = constant_cost_withGradients\n",
    "        # else:\n",
    "        #     self.cost_withGradients = cost_withGradients \n",
    "\n",
    "        # 2) THIS ONE IF THE EVALUATION COSTS DOES NOT MAKE SENSE\n",
    "        #\n",
    "        # if cost_withGradients == None:\n",
    "        #     self.cost_withGradients = constant_cost_withGradients\n",
    "        # else:\n",
    "        #     print('LBC acquisition does now make sense with cost. Cost set to constant.')  \n",
    "        #     self.cost_withGradients = constant_cost_withGradients\n",
    "\n",
    "\n",
    "    def _compute_acq(self,x):\n",
    "        \n",
    "        # --- DEFINE YOUR AQUISITION HERE (TO BE MAXIMIZED)\n",
    "        #\n",
    "        # Compute here the value of the new acquisition function. Remember that x is a 2D  numpy array  \n",
    "        # with a point in the domanin in each row. f_acqu_x should be a column vector containing the \n",
    "        # values of the acquisition at x.\n",
    "        #\n",
    "        \n",
    "        return f_acqu_x\n",
    "    \n",
    "    def _compute_acq_withGradients(self, x):\n",
    "        \n",
    "        # --- DEFINE YOUR AQUISITION (TO BE MAXIMIZED) AND ITS GRADIENT HERE HERE\n",
    "        #\n",
    "        # Compute here the value of the new acquisition function. Remember that x is a 2D  numpy array  \n",
    "        # with a point in the domanin in each row. f_acqu_x should be a column vector containing the \n",
    "        # values of the acquisition at x. df_acqu_x contains is each row the values of the gradient of the\n",
    "        # acquisition at each point of x.\n",
    "        #\n",
    "        # NOTE: this function is optional. If note available the gradients will be approxiamted numerically.\n",
    "        \n",
    "        return f_acqu_x, df_acqu_x\n",
    "            \n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
